/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.baumi.tankwars.entities;

import it.baumi.tankwars.Game;
import it.baumi.tankwars.util.Timer;
import it.baumi.tankwars.util.Line2d;
import org.lwjgl.opengl.GL11;
import it.baumi.tankwars.util.Vektor2d;
import java.util.ArrayList;

/**
 *
 * @author Michael Baumgartner
 */
public class BotEntity extends TankEntity {

    public BotEntity(Game game, float x, float y, float width, float height, float speed) {
        super(game, x, y, width, height, speed);
    }

    @Override
    public void draw() {

        GL11.glPushMatrix();
        GL11.glLoadIdentity();

        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2d(x - width / 2, y - height / 2);
        GL11.glVertex2d(x - width / 2, y + height / 2);
        GL11.glVertex2d(x + width / 2, y + height / 2);
        GL11.glVertex2d(x + width / 2, y - height / 2);

        GL11.glEnd();

        //panzer
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
//                GL11.glColor3f(0.7f, 0.7f, 0.7f);

        GL11.glVertex2d(x - width / 2, y - height / 2);
        GL11.glVertex2d(x - width / 2, y + height / 2);
        GL11.glVertex2d(x + width / 2, y + height / 2);
        GL11.glVertex2d(x + width / 2, y - height / 2);
        GL11.glEnd();

        TankEntity closestEnemy = findClosestEnemy();
        double targetX = closestEnemy.getX();
        double targetY = closestEnemy.getY();

        if (hasObstaclesBetween(closestEnemy)) {
            firing = false;
        } else {
            firing = true;
        }
        //mouse koordinaten in richtung des Ursprungs verschieben um richtungs vektor zu bekommen
        double dx = targetX - x;
        double dy = targetY - y;
        //distanz zwischen mousezeiger und panzer mittelpunkt berechnen
        double vektorBetrag = Math.sqrt(dx * dx + dy * dy);
        //Ortsvektor durch länge teilen und auf gewünschte länge anpassen und zum mittelpunkt des panzers verschieben
        double size = width > height ? width : height;
        double endpunktRohrX = x + dx / vektorBetrag * size * 1.1f;
        double endPunktRohrY = y + dy / vektorBetrag * size * 1.1f;
        GL11.glBegin(GL11.GL_LINES);
        GL11.glColor3f(0.0f, 1.0f, 1.0f);
        GL11.glVertex2d(x, y);
        GL11.glVertex2d(endpunktRohrX, endPunktRohrY);
        GL11.glEnd();

        //Shots
        if (isFiring()) {
            if ((Timer.getTime() - lastShootTime) > SHOOT_RATE) {
                if (shots.size() < MAX_SHOTS_COUNT) {
                    float degree = (float) Math.atan2(dy, dx);
                    shots.add(new ShotEntity(game, endpunktRohrX, endPunktRohrY, SHOT_WIDTH, SHOT_HEIGHT, SHOOT_SPEED, degree));
                    lastShootTime = Timer.getTime();
                }
            }
        }
        GL11.glPopMatrix();

        GL11.glPushMatrix();

        for (ShotEntity shot : shots) {
            if (shot.isDestroy()) {
                removeShots.add(shot);
            } else {
                shot.draw();
            }
        }

        for (ShotEntity removeShot : removeShots) {
            this.shots.remove(removeShot);
        }
        removeShots.clear();

        GL11.glPopMatrix();
    }

    public TankEntity findClosestEnemy() {
        TankEntity closestEnemy = null;
        double smalestDistance = -1;
        for (TankEntity tank : game.getTankList()) {
            if (!(tank instanceof BotEntity)) {
                Vektor2d v = new Vektor2d(x - tank.getX(), y - tank.getY());
                if (smalestDistance == -1) {
                    smalestDistance = v.length();
                    closestEnemy = tank;
                } else if (v.length() < smalestDistance) {
                    smalestDistance = v.length();
                    closestEnemy = tank;
                }
            }
        }
        return closestEnemy;
    }

    public boolean hasObstaclesBetween(TankEntity enemy) {

        Line2d line1 = new Line2d(x, enemy.getX(), y, enemy.getY());

        //linie 1
        GL11.glColor3f(0.0f, 0.0f, 0.2f);
        GL11.glBegin(GL11.GL_LINE_STRIP);
        GL11.glVertex2d(x, y);
        GL11.glVertex2d(enemy.getX(), enemy.getY());
        GL11.glEnd();

        Line2d line2 = null;
        ArrayList<WallEntity> walls = game.getWallList();
        for (WallEntity wall : walls) {
            line2 = new Line2d(wall.getxMin(), wall.getxMax(), wall.getyMin(), wall.getyMax());
            //linie2
            GL11.glColor3f(0.0f, 10.0f, 0.2f);
            GL11.glBegin(GL11.GL_LINE_STRIP);
            GL11.glVertex2d(wall.getxMin(), wall.getyMin());
            GL11.glVertex2d(wall.getxMax(), wall.getyMax());
            GL11.glEnd();
            if (line1.intersectsLine(line2)) {
                return true;
            }
        }
        return false;
    }

}
